package com.aoxon.cargo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.activity.n.PurMapActivity;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.cache.keyCache;
import com.aoxon.cargo.keydown.ExitApplication;
import com.aoxon.cargo.loader.FileLoader;
import com.aoxon.cargo.shared.SupIndexShared;
import com.aoxon.cargo.state.DataState;
import com.google.gson.Gson;

public class PurMoreActivity extends BaseActivity {

	private ImageView ivReturnButton;
	private ImageView ivStoreLogo;
	private ImageView ivCertification;

	private ProgressBar progressBar;

	private RelativeLayout rlNoteRecords;
	private RelativeLayout rlMarketMap;
	private RelativeLayout rlDialRecords;
	private RelativeLayout rlSuggestion;
	private RelativeLayout rlSupplier;
	private RelativeLayout rlSupplierLogged;

	private TextView tvTitle;
	private TextView tvLoginButton;
	private TextView tvRegisterButton;
	private TextView tvStoreName;

	private ExitApplication exitApplication = new ExitApplication(this);

	private FileLoader imageLoader = new FileLoader(100, 100, false);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setup();
	}

	private void setup() {
		setContentView(R.layout.pur_more);

		ivStoreLogo = (ImageView) findViewById(R.id.ivPurMoreLoggedLogo);
		ivCertification = (ImageView) findViewById(R.id.ivPurMoreLoggedCertification);
		ivReturnButton = (ImageView) findViewById(R.id.ivPublicTitleReturn);

		progressBar = (ProgressBar) findViewById(R.id.pbPurMoreLoggedLogo);

		rlNoteRecords = (RelativeLayout) findViewById(R.id.rlPurMoreNote);
		rlMarketMap = (RelativeLayout) findViewById(R.id.rlPurMoreMap);
		rlDialRecords = (RelativeLayout) findViewById(R.id.rlPurMoreDial);
		rlSuggestion = (RelativeLayout) findViewById(R.id.rlPurMoreSuggestion);
		rlSupplier = (RelativeLayout) findViewById(R.id.rlPurMoreSupplier);
		rlSupplierLogged = (RelativeLayout) findViewById(R.id.rlPurMoreLogged);

		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);
		tvStoreName = (TextView) findViewById(R.id.tvPurMoreLoggedName);
		tvLoginButton = (TextView) findViewById(R.id.tvPurMoreLogin);
		tvRegisterButton = (TextView) findViewById(R.id.tvPurMoreRegister);

		tvTitle.setText("更多");
		ivReturnButton.setVisibility(View.GONE);

		rlNoteRecords.setOnClickListener(myClickListener);
		rlMarketMap.setOnClickListener(myClickListener);
		rlDialRecords.setOnClickListener(myClickListener);
		rlSuggestion.setOnClickListener(myClickListener);
		rlSupplierLogged.setOnClickListener(myClickListener);

		tvLoginButton.setOnClickListener(myClickListener);
		tvRegisterButton.setOnClickListener(myClickListener);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		if (isLogged()) {
			rlSupplierLogged.setVisibility(View.VISIBLE);
			rlSupplier.setVisibility(View.GONE);
		} else {
			rlSupplier.setVisibility(View.VISIBLE);
			rlSupplierLogged.setVisibility(View.GONE);
		}
		super.onResume();
	}

	private boolean isLogged() {
		try {
			Supplier supplier = SupIndexShared.getSupplier();
			keyCache.supId = supplier.getSupId();
			keyCache.key = SupIndexShared.getKey();

			System.out.println(new Gson().toJson(supplier));

			tvStoreName.setText(supplier.getStoreName());
			if (supplier.getCerState() == DataState.SUCCESS) {
				ivCertification.setVisibility(View.VISIBLE);
			} else {
				ivCertification.setVisibility(View.GONE);
			}
			if (supplier.getSupLogo() != null
					&& supplier.getSupLogo().get(0).getPictureState() == DataState.SUCCESS) {
				String path = supplier.getSupLogo().get(0).getPictureUrl()
						+ "s.png";
				imageLoader.displayImage(path, ivStoreLogo, progressBar, false);
			} else {
				progressBar.setVisibility(View.GONE);
			}

			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	OnClickListener myClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = null;
			switch (v.getId()) {
			case R.id.tvPurMoreLogin:
				intent = new Intent(getBaseContext(), SupLoginActivity.class);
				break;
			case R.id.tvPurMoreRegister:
				intent = new Intent(getBaseContext(),
						SupRegisterFastActivity.class);
				break;

			case R.id.rlPurMoreLogged:
				// intent = new Intent(getBaseContext(),
				// SupIndexActivity.class);
				intent = new Intent(getBaseContext(), SupIndexActivity2.class);
				break;

			case R.id.rlPurMoreDial:
				intent = new Intent(getBaseContext(), PurDialListActivity.class);
				break;

			case R.id.rlPurMoreSuggestion:
				intent = new Intent(getBaseContext(),
						PurSuggestionActivity.class);
				break;

			case R.id.rlPurMoreNote:
				intent = new Intent(getBaseContext(), PurNoteListActivity.class);
				break;

			case R.id.rlPurMoreMap:
				PurMapActivity.LAUNCH_MODE = PurMapActivity.ONLY_MY_LOC;
				intent = new Intent(getBaseContext(), PurMapActivity.class);
				break;

			default:
				break;
			}
			startActivity(intent);
			onPause();
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		return exitApplication.onKeyDown(keyCode, event);
	}

}
